package com.now.printer.adapter;

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.now.printer.R;
import com.now.printer.layout.slant.NumberSlantLayout;
import com.now.printer.layout.straight.NumberStraightLayout;
import com.xiaopo.flying.puzzle.PuzzleLayout;
import com.xiaopo.flying.puzzle.SquarePuzzleView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by xudong on 2021/6/7.
 */

public class SelectAdapter extends RecyclerView.Adapter<SelectAdapter.PuzzleViewHolder> {

    private List<PuzzleLayout> layoutData = new ArrayList<>();
    private List<Bitmap> bitmapData = new ArrayList<>();
    private OnItemClickListener onItemClickListener;

    @Override
    public PuzzleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView =
                LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_photo, parent, false);
        return new PuzzleViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(PuzzleViewHolder holder, int position) {
        final PuzzleLayout puzzleLayout = layoutData.get(position);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    int theme = 0;
                    if (puzzleLayout instanceof NumberSlantLayout) {
                        theme = ((NumberSlantLayout) puzzleLayout).getTheme();
                    } else if (puzzleLayout instanceof NumberStraightLayout) {
                        theme = ((NumberStraightLayout) puzzleLayout).getTheme();
                    }
                    onItemClickListener.onItemClick(puzzleLayout, theme);
                }
            }
        });

        if (bitmapData == null) return;

        final int bitmapSize = bitmapData.size();
        if (bitmapSize > position) {
            holder.puzzleView.setImageBitmap(bitmapData.get(position));

        }

    }

    @Override
    public int getItemCount() {
        return bitmapData == null ? 0 : bitmapData.size();
    }

    public void refreshData(List<PuzzleLayout> layoutData, List<Bitmap> bitmapData) {
        this.layoutData = layoutData;
        this.bitmapData = bitmapData;

        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public static class PuzzleViewHolder extends RecyclerView.ViewHolder {

        ImageView puzzleView;

        public PuzzleViewHolder(View itemView) {
            super(itemView);
            puzzleView = (ImageView) itemView.findViewById(R.id.puzzle);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(PuzzleLayout puzzleLayout, int themeId);
    }
}

